package com.miniits.base.utils;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class BetServiceUtil {

    /**
     * 查看是否已封盘
     *
     * @param driver
     * @return
     */
    public static String closed(WebDriver driver) {
        String text = null;
        try {
            text = driver.findElement(By.xpath("//div[@class='open-time']/p/span[2]")).getText();
        } catch (Exception e) {
            try {
                text = driver.findElement(By.cssSelector("body > app-root > app-game > div > div.container-right > div.open-box > div.open-time > p > span:nth-child(2)")).getText();
            } catch (Exception e1) {
            }
        }
        return text;
    }

    /**
     * 获取封盘时间
     *
     * @param driver
     * @return
     */
    public static long closedTime(WebDriver driver) {
        String s = null;
        String m = null;
        long sleepTime = 0;
        String pNode = "body > app-root > app-game > div > div.container-right > div.open-box > div.open-time > div > ";

        try {
            s = driver.findElement(By.xpath("//div[@class='time']/span[5]")).getText();
        } catch (Exception e) {
            try {
                s = driver.findElement(By.cssSelector(pNode + "span:nth-child(7)")).getText();
            } catch (Exception e1) {
            }
        }
        try {
            m = driver.findElement(By.xpath("//div[@class='time']/span[6]")).getText();
        } catch (Exception e) {
            try {
                m = driver.findElement(By.cssSelector(pNode + "span:nth-child(8)")).getText();
            } catch (Exception e1) {
            }
        }

        if (null != s && !s.equals("0")) {
            if (null == m) {
                m = "0";
            }
            sleepTime = Integer.valueOf(s + m) * 1000 + 1500;
        }
        if (null != s && null != m && s.equals("0") && !m.equals("0")) {
            int mm = Integer.valueOf(m);
            sleepTime = mm * 1000 + 1500;
        }
        return sleepTime;
    }

    /**
     * 设置睡眠时间
     *
     * @return
     */
    public static void setSleepTime(String time) {

        if (StringUtils.isEmpty(time)) {
            time = "12:00:00";
        }
        /**
         * 判断是否到了12点
         */
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd " + time);
        String toTime = null;
        try {
            toTime = sdf.format(new Date());
        } catch (Exception e) {
        }

        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //加上时间
        try {
            Date toDate = sDateFormat.parse(toTime);
            long yusj = toDate.getTime() - System.currentTimeMillis();
            if (yusj > 0) {
                Thread.sleep(yusj);
            }
        } catch (Exception px) {
        }
    }

    /**
     * 获取12点时间
     *
     * @return
     */
    public static long get12(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd" + time);
        String toTime = null;
        try {
            toTime = sdf.format(new Date());
        } catch (Exception e) {
        }

        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date toDate = sDateFormat.parse(toTime);
            return toDate.getTime();
        } catch (Exception px) {
        }
        return 0;
    }

    public static Map<String, Integer> randomUtil(List<Integer> nums) {
        List<Integer> get = new ArrayList<>();
        Map<String, Integer> cacheBet = new HashMap<>();

//        冠军1
        Integer n = num(nums);
        cacheBet.put("gj", nums.get(n));
        nums = getNums(nums, nums.get(n));

//        亚军1
        n = num(nums);
        cacheBet.put("yj", nums.get(n));
        nums = getNums(nums, nums.get(n));

//        第三名1
        n = num(nums);
        cacheBet.put("dsm", nums.get(n));
        nums = getNums(nums, nums.get(n));

//        第四名2
        n = num(nums);
        cacheBet.put("dsm_", nums.get(n));
        nums = getNums(nums, nums.get(n));

        //        第5名2
        n = num(nums);
        cacheBet.put("dwm", nums.get(n));
        nums = getNums(nums, nums.get(n));

        //        第6名2
        n = num(nums);
        cacheBet.put("dlm", nums.get(n));
        nums = getNums(nums, nums.get(n));

        //        第7名3
        n = num(nums);
        cacheBet.put("dqm", nums.get(n));
        nums = getNums(nums, nums.get(n));

        //        第8名3
        n = num(nums);
        cacheBet.put("dbm", nums.get(n));
        nums = getNums(nums, nums.get(n));

        //        第9名4
        n = num(nums);
        cacheBet.put("djm", nums.get(n));
        nums = getNums(nums, nums.get(n));

        //        第10名5
        n = num(nums);
        cacheBet.put("dsm__", nums.get(n));
        nums = getNums(nums, nums.get(n));

        return cacheBet;
    }

    public static List<Integer> getNums(List<Integer> nums, Integer ignoreEle) {
        List<Integer> lists = new ArrayList<>();
        for (int i = 0; i < nums.size(); i++) {
            if (nums.get(i) != ignoreEle) {
                lists.add(nums.get(i));
            }
        }
        return lists;
    }

    public static Integer num(List<Integer> nums) {
        Random rand = new Random();
        int num = rand.nextInt(nums.size());
        return num;
    }

}
